package refactor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.standard.table.GdclFieldEnum;
import common.standard.table.PageEnum;
import common.vo.WorkOrderVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import refactor.entity.Gdcl;
import refactor.mapper.GdclMapper;
import refactor.service.IGdclService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author 杨健
 * @since 2019-08-19
 */
@Service
public class GdclServiceImpl extends ServiceImpl<GdclMapper, Gdcl> implements IGdclService {
    private GdclMapper workOrderMapper;

    @Autowired
    public void setGdclMapper(GdclMapper gdclMapper) {
        this.workOrderMapper = gdclMapper;
    }

    /**
     * 分页显示工单处理情况
     *
     * @param workOrder 工单查询的条件
     * @param regionId  区域id
     * @param pageNum   当前页码
     * @return 返回分页对象
     */
    @Override
    public IPage<Gdcl> pageList(WorkOrderVo workOrder, Integer regionId, Integer pageNum) {

        Page<Gdcl> gdclPage = new Page<>(pageNum, PageEnum.PAGE_SIZE.getValue());

        QueryWrapper<Gdcl> gdclQueryWrapper = new QueryWrapper<>();

        gdclQueryWrapper.eq(GdclFieldEnum.REGIONID.getFieldName(), regionId)
                .eq(GdclFieldEnum.TYPEID.getFieldName(), 7);

        // 两个不能同时为on
        if (!("on".equals(workOrder.getWcl()) && "on".equals(workOrder.getYcl()))) {
            if ("on".equals(workOrder.getWcl())) {
                gdclQueryWrapper.eq(GdclFieldEnum.RESULT.getFieldName(), 0);
            }
            if ("on".equals(workOrder.getYcl())) {
                gdclQueryWrapper.eq(GdclFieldEnum.RESULT.getFieldName(), 1);
            }
        }

        // 查询条件处理
        if (StringUtils.isNotBlank(workOrder.getCondition())) {
            gdclQueryWrapper.nested(i -> i.like(GdclFieldEnum.REALNAME.getFieldName(), workOrder.getCondition())
                    .or()
                    .like(GdclFieldEnum.DESCRIPTION.getFieldName(), workOrder.getCondition())
                    .or()
                    .like(GdclFieldEnum.CODE.getFieldName(), workOrder.getCondition()));
        }

        return workOrderMapper.selectPage(gdclPage, gdclQueryWrapper);
    }
}
